<!--权限分配-->
<template>
  <div>
    <el-table
      :data="tableData"
      style="width: 100%;margin-bottom: 20px;"
      row-key="action_id"
      border
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}">
      <el-table-column
        prop="action_id"
        label="id"
        sortable
        width="150"
       >
      </el-table-column>
      <el-table-column
        prop="action_name"
        label="姓名"
        sortable
        width="280">
      </el-table-column>
      <el-table-column
        prop="action_icon"
        label="图标"
        sortable
        width="280">
      </el-table-column>
      <el-table-column
        prop="url"
        label="路径"
        >
      </el-table-column>
      <el-table-column label="操作" width="230px" align="center">
        <template slot-scope="scope">
          <el-button
            size="mini"
            @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!--编辑权限信息-->
    <edit-action-dialog v-if="isShowEditAction" :isShowEditAction="isShowEditAction" :actionData="actionData" @closeDialog="closeDialog"></edit-action-dialog>
  </div>
</template>
<script>
  import {getAllAction,getActionByActionId,deleteActionById} from '@/api/permission'
  import editActionDialog from './component/editAction.vue'
  export default{
    components:{
      editActionDialog
    },
    data(){
      return {
        tableData: [],
        actionData:null,
        isShowEditAction:false
      }
    },
    mounted(){
        this.getAllActionTree();
    },
    methods:{
       //获取所有的权限信息
       getAllActionTree(){
         getAllAction().then(res=>{
             if(res.code == 200){
                 this.tableData = res.data;
                 for(let i = 0;i < this.tableData.length;i ++){
                   this.tableData[i].children = [];
                   for(let j = 0;j < this.tableData[i].list.length;j ++){
                     this.tableData[i].children.push(this.tableData[i].list[j])
                   }
                 }
             }
         });
       },

      //权限编辑
      handleEdit(index,row){
         //调用后台接口获取要编辑的角色信息
        getActionByActionId(row).then(res=>{
            if(res.code == 200){
                this.actionData = res.data;
                this.isShowEditAction = true
            }
        });
      },

      //权限的删除
      handleDelete(index,row){
        this.$confirm('是否删除该权限?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          deleteActionById(row).then(res=>{
            if(res.code == 200){
              this.$message({
                message: '删除成功',
                type: 'success'
              });
              this.getAllActionTree();
            }
          });
        }).catch(() => {

        });
      },

      //关闭弹出框
      closeDialog(type){
          if(type == "edit"){
            this.isShowEditAction = false
          }
          this.getAllActionTree();
      }

    }
  }
</script>
